<template>
  <div class="activity-add-container">
    <el-form ref="form" :model="form" :rules="option.rules" label-width="80px">
      <el-form-item label="课程名称" prop="name">
        <el-input v-model="form.courseName"/>
      </el-form-item>

      <el-form-item label="课程简介" prop="disc">
        <el-input v-model="form.courseDisc" type="textarea"></el-input>
      </el-form-item>
      <el-form-item label="课程价格">
        <el-input-number v-model="form.courseArrangePrice" :min="0"/>
      </el-form-item>
      <el-form-item label="课程人数" prop="disc">
        <el-input-number v-model="form.coursePeople" :min="0"/>
      </el-form-item>
      <el-form-item label="总学时">
        <el-input-number v-model="form.courseArrangeHours" :min="0"/>
      </el-form-item>
      <el-form-item label="可获积分">
        <!--        todo 修改字段-->
        <el-input-number v-model="form.courseScores" :min="0"/>
      </el-form-item>
      <el-form-item label="指定教师">
        <el-select v-model="form.teacherId" placeholder="请选择教师">
          <el-option
              v-for="item in option.teacherList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-upload
            :before-remove="beforeRemove"
            :file-list="option.fileList"
            :http-request="handleUpload"
            :limit="1"
            :on-exceed="handleExceed"
            :on-remove="handleRemove"
            action
            class="upload-demo"
            list-type="picture-card"
            name="singleFile"
        >
          <i class="el-icon-plus"></i>
          <div slot="tip" class="el-upload__tip">最多上传一张课程主图</div>
        </el-upload>
      </el-form-item>

      <el-form-item>
        <el-button type="success" @click="toSubmit(form)">立即发布</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  mounted () {
    this.handleGetTeacher();
  },
  data () {
    return {
      form: {
        courseName: "",
        courseDisc: "",
        courseArrangePrice: "",
        coursePeople: "",
        courseImage: "",
        teacherId: "",
        courseArrangeHours: 0,
        courseScores: 0,
      },
      option: {
        fileList: [],
        teacherList: [],
      },
    };
  },
  methods: {
    async handleGetTeacher () {
      const res = await this.$http.teacherAll({
        pageNum: 1,
        pageSize: 999,
      });
      if (res.code !== 0) return null;
      const teacher = this.option.teacherList;
      // 获得活动ID
      for (let {
        teacherName,
        teacherId
      } of res.data.list) {
        teacher.push({
          label: teacherName,
          value: teacherId,
        });
      }
    },

    async toSubmit (form) {
      for (let i in form) {
        if (form[i] === "") return this.$message.warning("请补充完整相关信息");
      }
      const res = await this.$http.manager_releasecourse(form);
      if (res.code) return this.$message.error("失败");
      // return this.$router.go(0);
    },
    confirmLocation (location) {
      // 确认位置信息
      this.form.address = JSON.stringify(location);
    },
    // 移除文件
    handleRemove (file, fileList) {
    },
    handleExceed (files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove (file) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    // 上传文件
    async handleUpload (file) {
      const params = new FormData();
      params.append("files", file.file);
      const res = await this.$http.uploadFile(params);
      if (res.code !== 200) return this.$message.error(res.message);
      this.form.courseImage = res.files[0];

    },

  },
};
</script>
